package com.example.service.impl;

import com.example.annotation.Log;
import com.example.constant.PasswordConstant;
import com.example.constant.StatusConstant;
import com.example.context.BaseContext;
import com.example.dto.EmployeeDTO;
import com.example.dto.EmployeeLoginDTO;
import com.example.entity.Employee;
import com.example.exception.AccountLockedException;
import com.example.exception.AccountNotFoundException;
import com.example.exception.PasswordErrorException;
import com.example.result.PageBeanResult;
import com.example.mapper.EmployeeMapper;
import com.example.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
@Slf4j
@Service
public class EmployeeImpl implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    /**
     * 用户登陆
     * @param employeeLoginDTO
     * @return
     */
    public EmployeeDTO login(EmployeeLoginDTO employeeLoginDTO)  {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        EmployeeDTO employeeDTO = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employeeDTO == null) {
            //账号不存在
            throw new AccountNotFoundException("账号不存在");
        }

        //对密码进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employeeDTO.getPassword())) {
            //密码错误
            throw new PasswordErrorException("密码错误");
        }
        if (employeeDTO.getStatus() == 0) {
            //账号被锁定
            throw new AccountLockedException("账号被锁定");
        }
        //3、返回实体对象
        return employeeDTO;
    }


    /**
     * 展示所有员工
     * @return
     */
    @Override
    public PageBeanResult showEmployee(Integer page,Integer pageSize) {

        PageHelper.startPage(page,pageSize);

        Page<EmployeeDTO> p=employeeMapper.showEmployee(page,pageSize);

        // 获取总条数
        long total=p.getTotal();
        // 获取查询结果
        List<EmployeeDTO> records=p.getResult();

        return new PageBeanResult(total,records);


        //return employeeMapper.showEmployee();
    }

    /**
     * 根据id删除员工
     * @param id
     */
    @Override
    public void deleteEmployee(Integer id) {

        employeeMapper.deleteEmployee(id);
    }

    /**
     * 新增员工
     *
     * @param emp
     */

    @Override
    public void addEmployee(EmployeeDTO emp) {
        //emp.setCreateTime(LocalDateTime.now());
        //emp.setUpdateTime(LocalDateTime.now());
        Employee employee = new Employee();
        //对象属性拷贝
        BeanUtils.copyProperties(emp, employee);

        //设置账号的状态，默认正常状态 1表示正常 0表示锁定
        employee.setStatus(StatusConstant.ENABLE);

        //设置密码，默认密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        //设置当前记录创建人id和修改人id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.addEmployee(employee);
    }

    /**
     * 根据username查询员工 并且分页展示
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBeanResult selectEmployee(String name, Integer page, Integer pageSize) {
        // 开始分页查询
        PageHelper.startPage(page,pageSize);
        Page<EmployeeDTO> p=employeeMapper.selectEmployee(name,page,pageSize);

        // 获取总条数
        long total=p.getTotal();
        // 获取查询结果
        List<EmployeeDTO> records=p.getResult();

        return new PageBeanResult(total,records);

    }

    /**
     * 根据id更改员工状态 1为启用 0为禁用
     * @param status
     * @param id
     */
    @Override
    @Log
    public void starOrStop(Integer status, Integer id) {
        employeeMapper.starOrStop(status,id);
    }

    /**
     * 修改员工数据回显 根据id
     * @param id
     * @return
     */
    @Override
    @Log
    public EmployeeDTO getById(Integer id) {

       return employeeMapper.getById(id);
    }

    /**
     * 修改员工
     * @param employeeDTO
     */

    @Override
    @Log
    public void updateEmployee(EmployeeDTO employeeDTO) {

        //employeeDTO.setUpdateTime(LocalDateTime.now());
        employeeMapper.updateEmployee(employeeDTO);
    }

    /**
     * 修改密码
     *
     * @param pwd
     * @param id
     */
    @Override
    @Log
    public void changePassWord(String pwd, Integer id) {

        employeeMapper.changePassWord(pwd,id);
    }

    /**
     * 批量删除员工
     * @param ids
     */
    @Override
    @Log
    public void deleteEmps(List<Integer> ids) {

        employeeMapper.deleteEmps(ids);
    }
}
